2D FFT of space/time signal

2D FFT of space/time signal#

import numpy as np
uconv = [30, 130., -70.]
kmax = 30.

def signal(x, t):
    f = 0.
    nwave = 50
    for u in uconv:
        for a, k, x0 in zip(np.random.rand(nwave), kmax*np.random.rand(nwave), np.random.rand(nwave)):
            f += a*np.sin(2*np.pi*k*(x-x0-u*t))
    return f

nt = 2048
nx = 512
ttot = .04
x = np.linspace(0., 1., nx)
t = np.linspace(0., ttot, nt)
xx, tt = np.meshgrid(x,t)
sig = signal(xx, tt)
#
import matplotlib.pyplot as plt
plt.rc('animation', html='jshtml')
import matplotlib.animation as pltanim
fig, ax = plt.subplots(1,1, figsize=(10,4))
line, = ax.plot(x, sig[0,:])
skip = 8 ; anim = pltanim.FuncAnimation(fig, lambda it: line.set_ydata(sig[skip*it,:]), frames=nt//skip, interval=50, repeat=True)
# skip = 8 ; anim = pltanim.FuncAnimation(fig, lambda it: ax.plot(x, sig[skip*it,:]), frames=nt//skip, interval=20, repeat=True)
anim
../_images/d047a739c16495035fcf0fbbe0b95940bb8ede9c43090d7a1db4e1a402f23148.png
sig *= np.hanning(nt)[:,np.newaxis] * np.hanning(nx)[np.newaxis,:]
mapf = np.abs(np.fft.fft2(sig))
mapf = np.fft.fftshift(mapf)
axek = -np.fft.fftshift(np.fft.fftfreq(sig.shape[1], d=1./nx))
axef = np.fft.fftshift(np.fft.fftfreq(sig.shape[0], d=ttot/nt))
#
fig, ax = plt.subplots(1,1, figsize=(14,6))
#
ax.pcolormesh(axek, axef, mapf, shading='auto')
ax.set_xlim(-1.2*kmax, 1.2*kmax)
ax.set_ylim(0, kmax*np.abs(np.max(uconv)))
(0.0, 3900.0)
../_images/4e9d10894e17183d3be61ef0e42af54b635b7fe280e3cc33178116b07018787c.png